package com.chen106106.sstest.common.session;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import net.spy.memcached.MemcachedClient;

public class MemcachedHttpSession
implements HttpSession, Serializable
{

public MemcachedHttpSession(String sessionId, MemcachedClient memcachedClient)
{
    updated = false;
    invalidated = false;
    this.sessionId = sessionId;
    this.memcachedClient = memcachedClient;
    memcachedClient.get(sessionId);
}

protected void update()
{
    if(invalidated)
        return;
    if(sessionValueMap.size() == 0)
        memcachedClient.set(sessionId, getMaxInactiveInterval(), Integer.valueOf(0));
    else
        memcachedClient.set(sessionId, getMaxInactiveInterval(), sessionValueMap);
    updated = true;
}

protected Map getValueMap()
{
    return sessionValueMap;
}

public Object getAttribute(String key)
{
    return sessionValueMap.get(key);
}

public Enumeration getAttributeNames()
{
    return new Enumerator(sessionValueMap.keySet(), true);
}

public void invalidate()
{
    memcachedClient.delete(sessionId);
    updated = true;
    invalidated = true;
}

public void removeAttribute(String key)
{
    sessionValueMap.remove(key);
    update();
}

public void setAttribute(String key, Object value)
{
    sessionValueMap.put(key, value);
    update();
}

public long getCreationTime()
{
    return 0L;
}

public String getId()
{
    return sessionId;
}

public long getLastAccessedTime()
{
    return 0L;
}

public int getMaxInactiveInterval()
{
    return maxInactiveInterval;
}

public ServletContext getServletContext()
{
    return servletContext;
}

/**
 * @deprecated Method getSessionContext is deprecated
 */

public HttpSessionContext getSessionContext()
{
    return null;
}

/**
 * @deprecated Method getValue is deprecated
 */

public Object getValue(String key)
{
    return getAttribute(key);
}

/**
 * @deprecated Method getValueNames is deprecated
 */

public String[] getValueNames()
{
    Set set = sessionValueMap.keySet();
    return (String[])set.toArray(new String[set.size()]);
}

public boolean isNew()
{
    return false;
}

/**
 * @deprecated Method putValue is deprecated
 */

public void putValue(String key, Object value)
{
    setAttribute(key, value);
}

/**
 * @deprecated Method removeValue is deprecated
 */

public void removeValue(String key)
{
    removeAttribute(key);
}

public void setMaxInactiveInterval(int maxInactiveInterval)
{
    this.maxInactiveInterval = maxInactiveInterval;
}

public boolean isUpdated()
{
    return updated;
}

public void setServletContext(ServletContext servletContext)
{
    this.servletContext = servletContext;
}

private static final long serialVersionUID = 6085773901132571541L;
private final MemcachedClient memcachedClient;
private final String sessionId;
private final Map sessionValueMap = new HashMap();
private int maxInactiveInterval;
private ServletContext servletContext;
private boolean updated;
private boolean invalidated;
}
